package com.itheima.controller;

import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.controller
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/students")
@Valid
public class StudentController {

    private StudentService studentService;

    @GetMapping
    public Result pageQuery(StudentQueryParam studentQueryParam){
        PageResult<Student> pageResult = studentService.pageQuery(studentQueryParam);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result add(@Valid @RequestBody Student student){
        studentService.add(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(studentService.getById(id));
    }

    @PutMapping
    public Result update(@RequestBody Student student){
        studentService.updateById(student);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        studentService.deleteById(id);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable Integer id, @Positive(message = "分数不能为空或负数") @PathVariable Integer score){
        studentService.violation(id,score);
        return Result.success();
    }
}
